<!--打印文件-->
<template>
    <div>
        <Modal v-model="printFileModal" :closable="false">
            <div slot="header">
                打印报关单
                <Icon class="f-fr" type="ios-close" @click="printFileCancel"/>
            </div>
            <p style="font-size:14px;color:#383A4A;margin-bottom:8px;">
                <span style="color:#F45050">*</span>选择打印文件类型
            </p>
            <CheckboxGroup v-model="printFileTypes">
                <Checkbox label="DECLARE">
                    <span>报关单</span>
                </Checkbox>
                <Checkbox label="DECLARE_GOODS">
                    <span>商品附加页</span>
                </Checkbox>
                <Checkbox label="DECLARE_CNT">
                    <span>集装箱附加页</span>
                </Checkbox>
            </CheckboxGroup>
            <div slot="footer">
                <Button type="primary" @click="previewHandle">打印预览</Button>
                <Button type="default" @click="printHandle">直接打印</Button>
                <Button type="default" @click="printFileCancel">取消</Button>
            </div>
        </Modal>

        <iframe
            style="visibility: hidden; height: 0; width: 0"
            v-for="(item, index) in printIframeUrlList"
            :key="index"
            :id="item.id"
            :src="item.url"
        >
        </iframe>
    </div>
</template>

<script>
import { onPrint } from '@/api/declare'
import $ from 'jquery'

export default {
    name: 'PrintFile',
    data () {
        return {
            printFileModal: false,
            declareId: '',
            printFileTypes: [],
            printIframeUrlList: []
        }
    },

    methods: {
        init (declareId) {
            this.declareId = declareId
            this.printFileModal = true
        },

        // 取消
        printFileCancel () {
            this.printFileModal = false
        },

        // 预览
        previewHandle () {
            if (this.printFileTypes.length === 0) {
                this.$Message.warning('请选择文件类型')
                return false
            }
            let obj = {
                declareId: this.declareId,
                types: this.printFileTypes.join()
            }
            onPrint(obj).then(res => {
                if (res.data.code === '0000') {
                    let list = res.data.data
                    if (list.length > 0) {
                        list.forEach(item => {
                            window.open('/static/pdf/web/viewer.html?file=' + item)
                        })
                    } else {
                        this.$Message.warning('文件不存在！')
                    }
                } else {
                    this.$Message.warning(res.msg)
                }
            })
        },

        // 直接打印
        printHandle () {
            this.printIframeUrlList = []

            if (this.printFileTypes.length === 0) {
                this.$Message.warning('请选择文件类型')
                return false
            }
            let obj = {
                declareId: this.declareId,
                types: this.printFileTypes.join()
            }
            onPrint(obj).then(res => {
                if (res.data.code === '0000') {
                    let list = res.data.data
                    if (list.length > 0) {
                        list.forEach((item, index) => {
                            this.printIframeUrlList.push({
                                id: 'printIframe' + index,
                                url: '/static/pdf/web/viewer.html?file=' + item
                            })
                        })

                        setTimeout(() => {
                            this.printIframeUrlList.forEach(item => {
                                let id = `#` + item.id
                                $(id)[0].contentWindow.print()
                            })
                        }, 1000)
                    } else {
                        this.$Message.warning('文件不存在！')
                    }
                } else {
                    this.$Message.warning(res.msg)
                }
            })
        }
    }
}
</script>
